package com.example.dzbsh.controller;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.*;

import com.example.dzbsh.model.Dept;
import com.example.dzbsh.service.DeptService;
import com.example.dzbsh.service.JiashuService;
import com.example.dzbsh.service.JingshangQingkuangService;
import com.example.dzbsh.util.CreateWord;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.example.dzbsh.service.RenyuanService;
import com.example.dzbsh.util.JsonResult;
import com.example.dzbsh.util.PageInfo;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("renyuan")
public class RenyuanController {

    @Value("${userfilepath}")
    private String userfilepath;
	@Resource
    RenyuanService ddService;

    @Resource
    JiashuService ddJiashuService;

	@Resource
    JingshangQingkuangService ddJingshangQingkuangService;
	@Resource
    DeptService ddDeptService;
	
    private Logger logger = LoggerFactory.getLogger(getClass());

    // 查询人员表包含的单位名称和机构名称码
    @RequestMapping("modifydept")
    public JsonResult<?> modifyDept(@RequestParam("olddeptname") String ppOldDeptname,
                                    @RequestParam("oldjigoumingchengma") String ppOldJigoumingchengma,
                                    @RequestParam("deptname") String ppDeptname,
                                    @RequestParam("jigoumingchengma") String ppJigoumingchengma){
        try {
            String mmGuanliDeptId=null;
            List<Dept> mmDeptList = ddDeptService.findByJigoumingchengma(ppJigoumingchengma);
            if(mmDeptList.size()==1){
                mmGuanliDeptId =   mmDeptList.get(0).getDeptid();
            }
            int  isok = ddService.modifyDept(ppDeptname,ppJigoumingchengma,mmGuanliDeptId,ppOldDeptname,ppOldJigoumingchengma);
            if(isok<0){
                return JsonResult.getErrorResult("保存失败");
            }else{
                return JsonResult.getSuccessResult("保存成功");
            }

        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.getErrorResult("renyuan/modifydept:error " + e.getMessage());
        }
    }

    // 查询人员表包含的单位名称和机构名称码
    @RequestMapping("findjigoumaanddeptname")
    public JsonResult<?> findJigoumaAndDeptname(@RequestParam("deptname") String ppDeptname,
                                   @RequestParam("jigoumingchengma") String ppJigoumingchengma){
        try {
            return JsonResult.getSuccessResult(ddService.findJigoumaAndDeptname( ppDeptname,ppJigoumingchengma));
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.getErrorResult("renyuan/findvaild:error " + e.getMessage());
        }
    }

    @RequestMapping("findVaild")
    public JsonResult<?> findvaild(@RequestParam("pageindex") int ppPageIndex,
                                   @RequestParam("pagesize") int ppPageSize){
        try {
            List<Map<String, Object>> mmList=ddService.findVaild(ppPageIndex,ppPageSize);
            Integer mmCount = ddService.getCountByisvalid();
            PageInfo mmPageInfo = new PageInfo(ppPageIndex, ppPageSize, mmCount);

            Map<String, Object> mmMap = new HashMap<String, Object>();
            mmMap.put("RenyuanList", mmList);
            mmMap.put("PageInfo", mmPageInfo);
            return JsonResult.getSuccessResult(mmMap);
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.getErrorResult("renyuan/findvaild:error " + e.getMessage());
        }
    }

    @RequestMapping("findOne")
    public JsonResult<?> findOne(@RequestParam("renyuanId") String pprenyuanID){
        try {
            return JsonResult.getSuccessResult(ddService.findOne(pprenyuanID));
        }catch (Exception e){
            e.printStackTrace();
            logger.error("renyuan/findOne:error",e);
            return JsonResult.getErrorResult("renyuan/findOne:error " + e.getMessage());
        }
    }

    @RequestMapping("find_custom_by_Valid")
    public JsonResult<?> findCustomRenyuanByvalid(@RequestParam("deptid") String ppDeptId,@RequestParam("truename") String ppTrueName,@RequestParam("renyuanleibie") String ppRenyuanleibie,
                                                  @RequestParam("shuliang") String ppShuliang,@RequestParam("paixu") String ppPaixu,@RequestParam("pageindex") int ppPageIndex,
                                                  @RequestParam("pagesize") int ppPageSize){
        try {
            List<Map<String, Object>> mmList=ddService.findCustomRenyuanByvalid(ppDeptId,ppTrueName,ppRenyuanleibie,ppShuliang,"",ppPaixu,ppPageIndex,ppPageSize);
            List<Map<String,Object>> mmRenyuanList = new ArrayList<>();
            for (int i = 0; i < mmList.size(); i++) {
                Map<String,Object> mmRenyuanMap =mmList.get(i);
                if (mmRenyuanMap!=null){
                    String mmRenyuanId = mmRenyuanMap.get("renyuanid")==null ? "" :mmRenyuanMap.get("renyuanid").toString();
                    int mmJingshangCount = ddJingshangQingkuangService.getCountByzhuangtaiAndRenyuanId(mmRenyuanId);
                    mmRenyuanMap.put("jingshangcount",mmJingshangCount);
                }
                mmRenyuanList.add(mmRenyuanMap);
            }
            Integer mmCount = ddService.getCustomCountByvalid(ppDeptId,ppTrueName,ppRenyuanleibie,ppShuliang,"",ppPageIndex,ppPageSize);
            PageInfo mmPageInfo = new PageInfo(ppPageIndex, ppPageSize, mmCount);

            Map<String, Object> mmMap = new HashMap<String, Object>();
            mmMap.put("RenyuanList", mmRenyuanList);
            mmMap.put("PageInfo", mmPageInfo);
            return JsonResult.getSuccessResult(mmMap);
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.getErrorResult("renyuan/findCustomRenyuanByvalid:error " + e.getMessage());
        }
    }

    @RequestMapping("select_depname_by_valid")
    public JsonResult<?> selectDepNameByValid() {
        try {

            return JsonResult.getSuccessResult(ddService.selectDepNameByValid());
        }catch (Exception e) {
            e.printStackTrace();
            return JsonResult.getErrorResult("renyuan/selectDepNameByValid:error " + e.getMessage());
        }
    }

    @RequestMapping("findallcount")
    public JsonResult<?> findAllCount(){
        try {
            Map<String, Object> mmCountMap = new HashMap<String,Object>();
            mmCountMap.put("renyuancount",ddService.getCountByisvalid());
            mmCountMap.put("jiashucount",ddJiashuService.getCountByisvalid());
            mmCountMap.put("jingshangcount",ddJingshangQingkuangService.getCountByisvalid());
            return JsonResult.getSuccessResult(mmCountMap);
        }catch (Exception e){
            e.printStackTrace();
            logger.error("renyuan/findOne:error",e);
            return JsonResult.getErrorResult("renyuan/findallcount:error " + e.getMessage());
        }
    }

    @RequestMapping("find_tongjibiao")
    public JsonResult<?> findTongjibiao() {
        try {

            return JsonResult.getSuccessResult(ddService.findTongjibiao());
        }catch (Exception e) {
            e.printStackTrace();
            return JsonResult.getErrorResult("renyuan/findTongjibiao:error " + e.getMessage());
        }
    }

    @RequestMapping("find_guifantongjibiao")
    public JsonResult<?> findGuifanTongjibiao(@RequestParam("deptid") String ppDeptId) {
        try {
            return JsonResult.getSuccessResult(ddService.findGuifanTongjibiao(ppDeptId));
        }catch (Exception e) {
            e.printStackTrace();
            return JsonResult.getErrorResult("renyuan/findGuifanTongjibiao:error " + e.getMessage());
        }
    }

    @RequestMapping("daochu_tongji")
    public void daochuTongji(HttpServletRequest request, HttpServletResponse response){

        try {
            Map<String,Object> mmResultMap = new HashMap<>();

            Map<String,Object> mmMap = ddService.findTongjibiao();
            String mmQuanbu = mmMap.get("quanbu")==null?"":mmMap.get("quanbu").toString();
            String mmShangjiangquanbu = mmMap.get("shangjiangquanbu")==null?"":mmMap.get("shangjiangquanbu").toString();
            String mmZhongjiangquanbu = mmMap.get("zhongjiangquanbu")==null?"":mmMap.get("zhongjiangquanbu").toString();
            String mmShaojiangzhengquanbu = mmMap.get("shaojiangzhengquanbu")==null?"":mmMap.get("shaojiangzhengquanbu").toString();
            String mmShaojiangfuquanbu = mmMap.get("shaojiangfuquanbu")==null?"":mmMap.get("shaojiangfuquanbu").toString();
            String mmDaxiaozhengquanbu = mmMap.get("daxiaozhengquanbu")==null?"":mmMap.get("daxiaozhengquanbu").toString();
            String mmDaxiaofuquanbu = mmMap.get("daxiaofuquanbu")==null?"":mmMap.get("daxiaofuquanbu").toString();


            String mmJingshang = mmMap.get("jingshang")==null?"":mmMap.get("jingshang").toString();
            String mmShangjiangjingshang = mmMap.get("shangjiangjingshang")==null?"":mmMap.get("shangjiangjingshang").toString();
            String mmZhongjiangjingshang = mmMap.get("zhongjiangjingshang")==null?"":mmMap.get("zhongjiangjingshang").toString();
            String mmShaojiangzhengjingshang = mmMap.get("shaojiangzhengjingshang")==null?"":mmMap.get("shaojiangzhengjingshang").toString();
            String mmShaojiangfujingshang = mmMap.get("shaojiangfujingshang")==null?"":mmMap.get("shaojiangfujingshang").toString();
            String mmDaxiaozhengjingshang = mmMap.get("daxiaozhengjingshang")==null?"":mmMap.get("daxiaozhengjingshang").toString();
            String mmDaxiaofujingshang = mmMap.get("daxiaofujingshang")==null?"":mmMap.get("daxiaofujingshang").toString();
            String mmShangxiaozhengJingshang = mmMap.get("shangxiaozhengjingshang")==null?"":mmMap.get("shangxiaozhengjingshang").toString();
            String mmShangxiaozhengQuanbu = mmMap.get("shangxiaozhengquanbu")==null?"":mmMap.get("shangxiaozhengquanbu").toString();

            mmResultMap.put("quanbu",mmQuanbu);
            mmResultMap.put("shangjiangquanbu",mmShangjiangquanbu);
            mmResultMap.put("zhongjiangquanbu",mmZhongjiangquanbu);
            mmResultMap.put("shaojiangzhengquanbu",mmShaojiangzhengquanbu);
            mmResultMap.put("shaojiangfuquanbu",mmShaojiangfuquanbu);
            mmResultMap.put("daxiaozhengquanbu",mmDaxiaozhengquanbu);
            mmResultMap.put("daxiaofuquanbu",mmDaxiaofuquanbu);
            mmResultMap.put("jingshang",mmJingshang);
            mmResultMap.put("shangjiangjingshang",mmShangjiangjingshang);
            mmResultMap.put("zhongjiangjingshang",mmZhongjiangjingshang);
            mmResultMap.put("shaojiangzhengjingshang",mmShaojiangzhengjingshang);
            mmResultMap.put("shaojiangfujingshang",mmShaojiangfujingshang);
            mmResultMap.put("daxiaozhengjingshang",mmDaxiaozhengjingshang);
            mmResultMap.put("daxiaofujingshang",mmDaxiaofujingshang);
            mmResultMap.put("shangxiaozhengjingshang",mmShangxiaozhengJingshang);
            mmResultMap.put("shangxiaozhengquanbu",mmShangxiaozhengQuanbu);

            if (userfilepath.endsWith("/")) {
                userfilepath = userfilepath.substring(0, userfilepath.length() - 1);
            }
            SimpleDateFormat mmDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            String mmSavePath = userfilepath + "/temp/jingshangqingkuang/"+mmDateFormat.format(new Date())+"/";
            File mmFold = new File(mmSavePath);
            if (!mmFold.isDirectory() || !mmFold.exists()) {
                mmFold.mkdirs();
            }

            CreateWord.createWord(userfilepath, mmSavePath,  "jingshangtongjibiao.xlsx", mmResultMap, request,
                    response,"jstj","1");

        } catch (Exception e) {
            e.printStackTrace();
            logger.error("renyuan/daochuTongji:error",e);

        }
    }
   

}
